"use strict";

/**
 * 文件列表控制器
 */
angular.module("app").controller("articleListCtrl", function($scope, $http, $location) {
	
	$scope.loading = false;
	$scope.html_body = $("html, body");
	
	$scope.articlePage = {};
	
	/**
	 * 初始化文章页面数据
	 */
	$scope.articlePage = {};
	$http.get("article/list/" + $("input[id='page.articleType']").val() + "/1",{
		cache	:	true
	}).success(function(d) {
		if(d.success){
			$scope.articlePage.articleType = d.ret.articleType;
			$scope.articlePage.datas = d.ret.page.datas;
			$scope.articlePage.hasNextPage = d.ret.page.hasNextPage;   
			$scope.articlePage.pageIndex = d.ret.page.pageIndex;
			$scope.articlePage.pageSize = d.ret.page.pageSize;
			
			/**
			 * 添加查找函数
			 */
			$scope.articlePage.datas.find = function(id){
				for(var i = 0; i < $scope.articlePage.datas.length; i++){
					var article = $scope.articlePage.datas[i];
					if(article.id == id){
						return article;
					}
				}				
				return null;
			}
		}
	});	
	
	/**
	 * 给评论按钮绑定提交事件
	 */
	$scope.do_comment = function(articleId){
		var article = $scope.articlePage.datas.find(articleId);
		var url = "/comment/dcomment";
		var content = $("#comment_texarea_" + articleId).val();
		$http.post(url, {
			articleId	:	articleId,
			content		:	content
		}).success(function(d){
			if(d.success){
				article.comments.datas.splice(0, 0, d.ret.comment);
				article.comments.maxId = d.ret.comment.id;
				$("#comment_texarea_" + articleId).val(""); 
			} else if(d.retCode == "NO_SESSION"){
				$("#login_modal").modal("show");
			}
        });
	}
	
	/**
	 * 给展开更多评论超链接绑定单击事件
	 */
	$scope.expand_more_comments = function (articleId){
		var article = $scope.articlePage.datas.find(articleId);
		var url = "/comment/list/after/" + article.comments.minId;
		$http.post(url, {
			articleId	:	articleId
		}).success(function(d){
			if(d.success){					
				for(var i = 0; i < d.ret.page.datas.length; i++){
					article.comments.datas.push(d.ret.page.datas[i]);
				}					
				article.comments.minId = d.ret.page.minId;
				article.comments.maxId = d.ret.page.maxId;
				article.comments.hasNextPage = d.ret.page.hasMoreRow;
			}
        });
	}
	
	/**
	 * 鼠标滚动事件
	 */
	$(document).scroll(function(){
        if ($(document).height() - $(window).scrollTop() - $(window).height()<100){ //到达底部100px时,加载新内容
        	$("#div_scroll_to_top").fadeIn();
        	setTimeout(load_more_article, 500);        	
        } else{
        	$("#div_scroll_to_top").fadeOut();
        }
	});
	
	/**
	 * 回到顶部
	 */
	$("#btn_scroll_to_top").click(function(){
		$scope.html_body.animate({scrollTop:0}, "slow");
	});
	
	/**
	 * 加载更多文章
	 */
	function load_more_article(){
		if($scope.articlePage.hasNextPage){
			if($scope.loading == false){
				$scope.loading = true;
				var articleType = $scope.articlePage.articleType;
				var pageIndex =  $scope.articlePage.pageIndex  + 1;
				var url = "/article/list/" + articleType + "/" + pageIndex;
				$http.get(url).success(function(d){
					if(d.success){
						for(var i = 0; i < d.ret.page.datas.length; i++){
							$scope.articlePage.datas.push(d.ret.page.datas[i]);
						}
						$scope.articlePage.pageIndex = d.ret.page.pageIndex;
						$scope.articlePage.hasNextPage = d.ret.page.hasNextPage;					
					}
					$scope.loading = false;
				});
			}
		}
	}
	
	/**
	 * 给文章绑定收藏事件
	 */
	$scope.do_favorite_or_not = function(articleId){
		var article = $scope.articlePage.datas.find(articleId);
		var url = null;
		if(article.curuser_favorite){
			url = "/favorite/del/" + articleId;
		} else{
			url = "/favorite/add/" + articleId;
		}
		$http.get(url).success(function(d){
			if(d.success){
				article.curuser_favorite = !article.curuser_favorite;					
			} else if(d.retCode == "NO_SESSION"){
				$("#login_modal").modal("show");
			}
			if(d.success && d.retCode != "NO_SESSION"){
				$.alert({
					title			: 	null,
				    content			: 	d.message,
				    animation		: 	"top",
				    closeIcon		: 	true,
				    confirmButton	:	null,
				    theme			:	"material",
				    keyboardEnabled	:	true,
				    columnClass		: 	"col-sm-offset-5 col-sm-3"
				});
			}
        });
	}
	
	/**
	 * 给文章绑定收藏事件
	 */
	$scope.do_thumbup_or_not = function(articleId){
		var article = $scope.articlePage.datas.find(articleId);
		var url = null;
		if(article.curuser_up){
			url = "/thumb/unup/" + articleId;
		} else{
			url = "/thumb/up/" + articleId;
		}
			
		$http.get(url).success(function(d){
			if(d.success){
				article.curuser_up = !article.curuser_up;
			} else if(d.retCode == "NO_SESSION"){
				$("#login_modal").modal("show");
			}
        });
	}
	
}).filter("trustAsHtml", function($sce) {
	
	/**
	 * 输出html
	 */
	return function(text) {
		return $sce.trustAsHtml(text);
	}
});